package cn.lxb.goldengate.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.*;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.*;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.*;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.*;
import static org.springframework.cloud.gateway.server.mvc.filter.LoadBalancerFilterFunctions.*;

@SpringBootApplication
public class GoldengateApplication {

	public static void main(String[] args) {
		SpringApplication.run(GoldengateApplication.class, args);
	}
	
	@Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
		return route("user_service")
				.route(path("/user/**"), http())
				.filter(lb("goldengate-demo-user"))
					.before(stripPrefix(1))
					.before(modifyRequestBody(String.class, String.class, MediaType.TEXT_PLAIN_VALUE,
								(request, s) -> "mod "+s ))
					.build();
    }

}
